package com.example.mybatisplusdemo.Controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.mybatisplusdemo.Result.Result;
import com.example.mybatisplusdemo.Service.AdminService;
import com.example.mybatisplusdemo.Service.BusinessService;
import com.example.mybatisplusdemo.Service.CateService;
import com.example.mybatisplusdemo.Service.GoodService;
import com.example.mybatisplusdemo.pojo.Business;
import com.example.mybatisplusdemo.pojo.Cate;
import com.example.mybatisplusdemo.pojo.Good;
import net.sf.jsqlparser.statement.select.Limit;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

@RestController
@RequestMapping("/api")
@CrossOrigin
public class GoodController {
    @Resource
    AdminService adminService;
    @Resource
    GoodService goodService;
    @Resource
    CateService cateService;
    @Resource
    BusinessService businessService;
    /**
     * 获取所有商品
     * @return
     */
    @GetMapping("/getgoods")
    public Result getgoods(){
        List<Good> list = goodService.list(new QueryWrapper<Good>().orderByDesc("id"));
        list.forEach(item->{
            item.setBusname(businessService.getOne(
                    new QueryWrapper<Business>().
                            eq("id",item.getBusid())).getNike());
            item.setCatename(cateService.getOne(
                    new QueryWrapper<Cate>().
                            eq("id",item.getCateid())).getCatename());
        });
        return new Result(200,"获取成功",list);
    }

    /**
     * 检索随机10个商品
     * @return
     */
    @GetMapping("/getgoodsinfirst")
    public Result getgoodsinfirst(){
        List<Good> list = goodService.list(new QueryWrapper<Good>().orderByDesc("id").last("limit 10"));
        list.forEach(item->{
            item.setBusname(businessService.getOne(
                    new QueryWrapper<Business>().
                            eq("id",item.getBusid())).getNike());
            item.setCatename(cateService.getOne(
                    new QueryWrapper<Cate>().
                            eq("id",item.getCateid())).getCatename());
        });
        return new Result(200,"获取成功",list);
    }
    /**
     * 获取对应商家所有商品
     * @return
     */
    @GetMapping("/getgoodsbybus")
    public Result getgoodsbybus(@RequestParam("busid") String busid){
        List<Good> list = goodService.list(new QueryWrapper<Good>().eq("busid",busid).orderByDesc("id"));
        list.forEach(item->{
            item.setBusname(businessService.getOne(
                    new QueryWrapper<Business>().
                            eq("id",item.getBusid())).getNike());
            item.setCatename(cateService.getOne(
                    new QueryWrapper<Cate>().
                            eq("id",item.getCateid())).getCatename());
        });
        return new Result(200,"获取成功",list);
    }

    /**
     * 通过id查询商品
     * @param id
     * @return
     */
    @GetMapping("/getgoodsbyid")
    public Result getgoodsbyid(@RequestParam("id") String id){
        Good good = goodService.getOne(new QueryWrapper<Good>().eq("id",id));
        good.setBusname(businessService.getOne(
                new QueryWrapper<Business>().
                        eq("id",good.getBusid())).getNike());
        good.setCatename(cateService.getOne(
                new QueryWrapper<Cate>().
                        eq("id",good.getCateid())).getCatename());
        return new Result(200,"查询成功",good);
    }


    /**
     * 通过商家和模糊查询
     * @param busid
     * @param words
     * @return
     */
    @GetMapping("/getgoodsbybusandname")
    public Result getgoodsbybusandname(@RequestParam("busid") String busid,
                                       @RequestParam("words") String words){
        List<Good> list = goodService.list(new QueryWrapper<Good>().like("name",words).eq("busid",busid).orderByDesc("id"));
        list.forEach(item->{
            item.setBusname(businessService.getOne(
                    new QueryWrapper<Business>().
                            eq("id",item.getBusid())).getNike());
            item.setCatename(cateService.getOne(
                    new QueryWrapper<Cate>().
                            eq("id",item.getCateid())).getCatename());
        });
        return new Result(200,"获取成功",list);
    }


    /**
     * 前台模糊查询商品
     * @param words
     * @return
     */
    @GetMapping("/getgoodsbyname")
    public Result getgoodsbyname(@RequestParam("words") String words){
        List<Good> list = goodService.list(new QueryWrapper<Good>().like("name",words).orderByDesc("id"));
        list.forEach(item->{
            item.setBusname(businessService.getOne(
                    new QueryWrapper<Business>().
                            eq("id",item.getBusid())).getNike());
            item.setCatename(cateService.getOne(
                    new QueryWrapper<Cate>().
                            eq("id",item.getCateid())).getCatename());
        });
        return new Result(200,"获取成功",list);
    }

    /**
     * 管理员删除方法
     * @param token
     * @param id
     * @return
     */
    @PostMapping("/admindeletegood")
    public Result admindeletegood(@RequestParam("token") String token,
                                  @RequestParam("id") String id){
        if (!adminService.checktoken(token)){
            return new Result(400,"权限不足");
        }
        Good good = goodService.getOne(new QueryWrapper<Good>().eq("id",id));
        String cateid = good.getCateid();
        boolean isDelete = goodService.remove(new QueryWrapper<Good>().eq("id",id));
        if (isDelete){
            Cate cate = cateService.getOne(new QueryWrapper<Cate>().eq("id",cateid));
            cate.setNum(String.valueOf(Integer.parseInt(cate.getNum()) - 1));
            cateService.update(cate,new QueryWrapper<Cate>().eq("id",cateid));
            return new Result(200,"删除成功");
        }
        else{return new Result(400,"删除失败");}
    }

    /**
     * 商家删除方法
     * @param token
     * @param id
     * @return
     */
    @PostMapping("/busdeletegood")
    public Result busdeletegood(@RequestParam("token") String token,
                                @RequestParam("id") String id){
        if (!businessService.checktoken(token)){
            return new Result(400,"权限不足");
        }
        Good good = goodService.getOne(new QueryWrapper<Good>().eq("id",id));
        String cateid = good.getCateid();
        boolean isDelete = goodService.remove(new QueryWrapper<Good>().eq("id",id));
        if (isDelete){
            Cate cate = cateService.getOne(new QueryWrapper<Cate>().eq("id",cateid));
            cate.setNum(String.valueOf(Integer.parseInt(cate.getNum()) - 1));
            cateService.update(cate,new QueryWrapper<Cate>().eq("id",cateid));
            return new Result(200,"删除成功");
        }
        else{return new Result(400,"删除失败");}
    }

    /**
     * 商家添加商品
     * @param token
     * @param name
     * @param cateid
     * @param busid
     * @param img
     * @param detail
     * @param price
     * @return
     */
    @PostMapping("/addgood")
    public Result addgood(@Valid @NotBlank(message = "token不能为空")@RequestParam("token") String token,
                          @Valid @NotBlank(message = "商品名不能为空")@RequestParam("name") String name,
                          @Valid @NotBlank(message = "分类id不能为空")@RequestParam("cateid") String cateid,
                          @Valid @NotBlank(message = "商家id不能为空")@RequestParam("busid") String busid,
                          @Param("img") String img,
                          @Valid @NotBlank(message = "详情不能为空")@RequestParam("detail") String detail,
                          @Valid @NotBlank(message = "价格不能为空")@RequestParam("price") String price){
        if (!businessService.checktoken(token)){
            return new Result(400,"权限不足");
        }
        Good good = new Good();
        good.setName(name);
        good.setCateid(cateid);
        good.setBusid(busid);
        good.setImg(img);
        good.setDetail(detail);
        good.setPrice(price);
        boolean isSave = goodService.save(good);
        if (isSave) {
            Cate cate = cateService.getOne(new QueryWrapper<Cate>().eq("id",cateid));
            cate.setNum(String.valueOf(Integer.parseInt(cate.getNum())+1));
            cateService.update(cate,new QueryWrapper<Cate>().eq("id",cateid));
            return new Result(200,"添加成功");
        }
        else{return new Result(400,"添加失败");}
    }

    /**
     * 管理员编辑商品
     * @param token
     * @param id
     * @param name
     * @param cateid
     * @param busid
     * @param img
     * @param detail
     * @param price
     * @param ishot
     * @return
     */
    @PostMapping("/adminupdategood")
    public Result adminupdategood(@Valid @NotBlank(message = "token不能为空")@RequestParam("token") String token,
                                  @Valid @NotBlank(message = "id不能为空")@RequestParam("id") String id,
                                  @Valid @NotBlank(message = "商品名不能为空")@RequestParam("name") String name,
                                  @Valid @NotBlank(message = "分类id不能为空")@RequestParam("cateid") String cateid,
                                  @Valid @NotBlank(message = "商家id不能为空")@RequestParam("busid") String busid,
                                  @Param("img") String img,
                                  @Valid @NotBlank(message = "详情不能为空")@RequestParam("detail") String detail,
                                  @Valid @NotBlank(message = "价格不能为空")@RequestParam("price") String price,
                                  @Valid @NotBlank(message = "热门状态不能为空")@RequestParam("ishot") String ishot){
        if (!adminService.checktoken(token)){
            return new Result(400,"权限不足");
        }
        Good good = goodService.getOne(new QueryWrapper<Good>().eq("id",id));
        good.setName(name);
        good.setCateid(cateid);
        good.setBusid(busid);
        good.setImg(img);
        good.setDetail(detail);
        good.setPrice(price);
        good.setIshot(ishot);
        boolean isUpdate = goodService.update(good,new QueryWrapper<Good>().eq("id",id));
        if (isUpdate) {return new Result(200,"更新成功");}
        else{return new Result(400,"更新失败");}
    }

    /**
     * 商家编辑商品
     * @param token
     * @param id
     * @param name
     * @param cateid
     * @param busid
     * @param img
     * @param detail
     * @param price
     * @param ishot
     * @return
     */
    @PostMapping("/busupdategood")
    public Result busupdategood(@Valid @NotBlank(message = "token不能为空")@RequestParam("token") String token,
                                @Valid @NotBlank(message = "id不能为空")@RequestParam("id") String id,
                                @Valid @NotBlank(message = "商品名不能为空")@RequestParam("name") String name,
                                @Valid @NotBlank(message = "分类id不能为空")@RequestParam("cateid") String cateid,
                                @Valid @NotBlank(message = "商家id不能为空")@RequestParam("busid") String busid,
                                @Param("img") String img,
                                @Valid @NotBlank(message = "详情不能为空")@RequestParam("detail") String detail,
                                @Valid @NotBlank(message = "价格不能为空")@RequestParam("price") String price,
                                @Valid @NotBlank(message = "热门状态不能为空")@RequestParam("ishot") String ishot){
        if (!businessService.checktoken(token)){
            return new Result(400,"权限不足");
        }
        Good good = goodService.getOne(new QueryWrapper<Good>().eq("id",id));
        good.setName(name);
        good.setCateid(cateid);
        good.setBusid(busid);
        good.setImg(img);
        good.setDetail(detail);
        good.setPrice(price);
        good.setIshot(ishot);
        boolean isUpdate = goodService.update(good,new QueryWrapper<Good>().eq("id",id));
        if (isUpdate) {return new Result(200,"更新成功");}
        else{return new Result(400,"更新失败");}
    }

    @GetMapping("/getgoodsbycate")
    public Result getgoodsbycate(@Param("cateid") String cateid){
        if (cateid == null){
            return new Result(200,"get",goodService.list(new QueryWrapper<Good>().orderByDesc("id")));
        }else{
            return new Result(200,"get",goodService.list(new QueryWrapper<Good>().eq("cateid",cateid).orderByDesc("id")));
        }
    }
}
